<?php


namespace App\Admin\Actions;


use App\Models\AdminUser;
use Illuminate\Database\Eloquent\Collection;

class BatchAction extends \Encore\Admin\Actions\BatchAction
{
    public function authorize(AdminUser $user, Collection $collection)
    {
        foreach ($collection as $model) {
            if($user->role_type != 1 && $user->id != $model->user_id){
                return false;
            }
        }
        return true;
    }

    protected function rename()
    {
        return $this->name;
    }

    public function render()
    {
        $this->name = $this->rename();
        return parent::render(); // TODO: Change the autogenerated stub
    }

    public function actionScript()
    {
        $warning = __('未选择数据!');

        return <<<SCRIPT
        var key = $.admin.grid.selected();

        if (key.length === 0) {
            $.admin.toastr.warning('{$warning}', '', {positionClass: 'toast-top-center'});
            return ;
        }

        Object.assign(data, {_key:key});
SCRIPT;
    }

    public function aHtml($name, $bgClass = 'primary'): string
    {
        return <<<HTML
<a class="{$this->getElementClass()}  btn btn-sm btn-{$bgClass}">{$name}</a>
HTML;
    }

    public function __toString()
    {
        return $this->render();
    }


}
